package map.landC;

import java.util.Scanner;

public class Main {
    static int[][] dir = new int[][]{{0,1},{1,0},{-1,0},{0,-1}};
    static int landC = 0;
    public static void findC(int[][] lands, int x, int y){
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if (nextX < 0 || nextY < 0 || nextX >= lands.length || nextY >= lands[0].length || lands[nextX][nextY] == 0){
                landC++;
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[][] lands = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                lands[i][j] = sc.nextInt();
            }
        }
        boolean[][] visited = new boolean[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (!visited[i][j] && lands[i][j] == 1){
                    visited[i][j] = true;
                    findC(lands, i, j);
                }
            }
        }
        System.out.println(landC);
    }
}
